package cn.mesmile.juc.pool;

import java.util.concurrent.*;

/**
 * @author zb
 * @date 2021/8/17 23:47
 * @Description
 */
public class CustomThreadPoolDemo {

    /**
     *      int corePoolSize, // 常驻线程数量
     *      int maximumPoolSize, // 最大线程数量
     *      long keepAliveTime, // 线程存活时间
     *      TimeUnit unit, // 线程存活时间
     *      BlockingQueue<Runnable> workQueue,// 阻塞队列
     *      ThreadFactory threadFactory, // 线程工厂
     *      RejectedExecutionHandler handler) // 拒绝策略
     */
    public static void main(String[] args) {
        ThreadFactory threadFactory = Executors.defaultThreadFactory();
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                2,
                5,
                60L,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(6),
                threadFactory,
                new ThreadPoolExecutor.AbortPolicy()
        );
        try {
            System.out.println("111111111111111111111111111111111");
            for (int i = 0; i < 10; i++) {
                final int f = i;
                threadPoolExecutor.execute(()->{
                    try {
                        Thread.sleep(500L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        throw new RuntimeException("111");
                    }
                    if (f == 3){
                        // 这里抛出异常不会影响其他线程
                        int a = 1/0;
                    }
                    System.out.println(Thread.currentThread().getName()+" 处理业务");
                });
            }
        } finally {
            threadPoolExecutor.shutdown();
        }
        System.out.println("22222222222222222222222222222222");

    }
}
